﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _Sql查询结果转List
{
    public class StudentDAL
    {
        private Sutdent GetDtl(IDataReader reader)
        {
            Sutdent sutdent = new Sutdent();
            sutdent.Id = (int)reader["Id"];
            sutdent.Name = (string)reader["Name"];
            sutdent.Sex = (string)reader["Sex"];
            sutdent.StuNum = (string)reader["StuNum"];
            sutdent.Class = (string)reader["Class"];
            sutdent.ClassNum = (string)reader["ClassNum"];
            return sutdent;
        }

        public List<Sutdent> GetAllList(string ConnStr, System.Data.CommandType type, string sql, params SqlParameter[] para)
        {
            List<Sutdent> list = new List<Sutdent>();

            using (SqlConnection conn = new SqlConnection(ConnStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = type;
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(para);
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            Sutdent sutdent = GetDtl(sdr);
                            list.Add(sutdent);
                        }
                    }

                }
            }

            return list;
        }
    }
}
